package Algorithms;

public class BinarySearch {
	
	int []sortedArray;
	int length;
	
	public int search(int []array, int key){
		
		this.sortedArray = array;
		this.length = array.length;
		return binarysearch(sortedArray, 0, length - 1, key);
	}
	
	private int binarysearch(int[] sArray, int first, int upto, int key) {
	    
	    if (first < upto) {
	        int mid = first + (upto - first) / 2;  // Compute mid point.
	        if (key < sArray[mid]) {
	            return binarysearch(sArray, first, mid, key);
	            
	        } else if (key > sArray[mid]) {
	            return binarysearch(sArray, mid+1, upto , key);
	            
	        } else {
	            return mid;   // Found it.
	        }
	    }
	    return -(first + 1);  // Failed to find key
	}
}
